import Axios from "axios";
import httpStatus from "http-status";
import errors from "restify-errors";

export const extractFilename = (url: string) => {
  return url.split("/").pop() ?? "unknown";
};

export const downloadFile = async (url: string) => {
  const resp = await Axios.get(url, {
    responseType: "stream"
  });

  if (resp.status !== httpStatus.OK) {
    throw new errors.InternalError("WuLai: failed to get response");
  }

  return resp.data as ReadableStream;
};
